package com.hyfd.deal.Flow;

import java.security.KeyFactory;
import java.security.MessageDigest;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.Cipher;

import org.apache.commons.codec.binary.Hex;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.hyfd.common.GenerateData;
import com.hyfd.common.utils.Base64Utils;
import com.hyfd.common.utils.MapUtils;
import com.hyfd.common.utils.OPSignatureUtil;
import com.hyfd.common.utils.ToolDateTime;
import com.hyfd.common.utils.ToolHttp;
import com.hyfd.common.utils.XmlUtils;
import com.hyfd.deal.BaseDeal;

/**
 * BeiMi单号码流量充值接口
 * 
 * @author cxj
 *
 */
public class HaoBaiFlowDeal implements BaseDeal
{
    Logger log = Logger.getLogger(getClass());
    
    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> deal(Map<String, Object> order)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        int flag = -1;
        String privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAIkvcjD/7ecMFqF5ulVUV7cSRDlacXGL0793ugQP+PYuI7giHVvQRZXOqRf9KtR46lBDIi3BbcPmiHDyMSddsc+bCCqkmaXq41AVVlB54XwH3WeCeBFm/eRlcKqqvAKBGkOUJ7qfQTArlwAxr9vxF4aWNNzD4TnZgN7Gvu+sRPvnAgMBAAECgYBui/A98g54j/IwnxH7LuMcIDkf6nTeJeo1MTqRiXl6j8Wy3LLByLw6MvVp4lXAL8vJb7+8FVHpefza3x+i9EqSrUHc8jQ12+n9SrYBPTwg5xMCj+QS43LsGF6hoNyUCJyVZl6iBt2Baytdbaa3ypGf/JcOVlbmNKF3cvHfOrHtqQJBAMi/+yfAJyekmXE4Ja3BDOkVg0dxOCW6bHVETTxIxdBXdYMiYA7QzJ/vKy/bEkC/j29pvc1naHM+M3iBrA0Qx50CQQCu8PbB4vZEUcXQrKT3lSBs43wwkBTswOskUGau0Zp2xcCNTOGVV5LAdwyMqEa0Lki+QedhNhTg6uy0rLJybBRTAkA29lD2HibaNLeKBvxR6RP20NwDeF0N/09of83Bno8CLp/Oyl+QTtxnQV0KmikSLyA/Oa0sio5PVw1ShfYsEZcFAkEAlqMG140HYWzIWRdQNaBDe3+mwGpQmkzELVDOGlqCFyPQ2dsJo/70bqYUOEnuF9iC6tD029AlykZ/7FOIlN5b4QJBAKJHdTnpEJ0g74pKNRVixk5a9CkkqSY6e6tB5A1UA7L34fVA5iD2plOz3PY0AqDSRDFUe1ZATgBaX8ZpDbpKZAc=";
        try
        {
            // 业务类型
            String bizType = "1";
            // 适用范围
            String roamType = "nation";
            // 手机号
            String phone = (String)order.get("phone");
            // 平台订单号
            String curids =
                ToolDateTime.format(new Date(), "yyyyMMddHHmmssSSS") + phone + GenerateData.getIntData(9, 4);
            String custOrderId = curids;
            map.put("orderId", curids);
            // 账号
            Map<String, Object> channel = (Map<String, Object>)order.get("channel");// 获取通道参数
            String linkUrl = (String)channel.get("link_url");// 充值地址
            String defaultParameter = (String)channel.get("default_parameter");// 默认参数
            Map<String, String> paramMap = XmlUtils.readXmlToMap(defaultParameter);
            String terminalName = paramMap.get("terminalName");// 账户名
            String callbackUrl = paramMap.get("callbackUrl");// 平台的appKey
            // 0 漫游流量 1非漫游流量，不带改参数时默认为0
            // 套餐
            String spec = new Double(order.get("value") + "").intValue() + "";
            
            Map<String, String> param = new HashMap<String, String>();
            
            String timeStamp = ToolDateTime.format(new Date(), "yyyyMMddHHmmssSSS");
            String signature = "";
            
            param.put("terminalName", terminalName);
            param.put("customerOrderId", curids);
            param.put("phoneNo", phone);
            param.put("orderType", bizType);
            param.put("spec", spec);
            param.put("scope", roamType);
            param.put("callbackUrl", callbackUrl);
            param.put("timeStamp", timeStamp);
            
            try
            {
                signature = OPSignatureUtil.sign(param, privateKey);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            StringBuffer reqUrl = new StringBuffer();
            
            reqUrl.append(linkUrl)
                .append("?")
            .append("terminalName=" + terminalName)
            .append("&customerOrderId=" + curids)
            .append("&phoneNo=" + phone)
            .append("&orderType=" + bizType)
            .append("&spec=" + spec)
            .append("&scope=" + roamType)
            .append("&callbackUrl=" + callbackUrl)
            .append("&timeStamp=" + timeStamp)
            .append("&signature=" + signature);
            
            // get请求
            String result = ToolHttp.get(false, reqUrl.toString());
            // 判断是否成功
            if (result != null)
            {
                JSONObject jObject = JSONObject.parseObject(result);
                String code = jObject.getString("code");
                String message = jObject.getString("message");
                map.put("resultCode", code + ":" + message);
                if ("0".equals(code))
                {
                    flag = 1;
                }
                else
                {
                    flag = 0;
                }
            }
        }
        catch (Exception e)
        {
            log.error("贝咪指通流量出错" + e + MapUtils.toString(order));
        }
        map.put("status", flag);
        return map;
    }
    
    
}
